package com.longrise.android.byjk.plugins.hra;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.Html;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longrise.android.byjk.R;
import com.longrise.android.byjk.plugins.hra.hratabsecond.MineAppointFragment;
import com.longrise.android.byjk.plugins.hra.hratabsecond.OrderSearchActivity;
import com.longrise.android.byjk.plugins.hra.hratabthird.EndorseSearchActivity;
import com.longrise.android.byjk.plugins.hra.hratabthird.MineEndorseFragment;
import com.longrise.common.base.BaseActivity2;

/**
 * Created by Administrator on 2018/1/12.
 * 搜索界面
 */

public class HraSearchActivity extends BaseActivity2 implements View.OnClickListener, TextWatcher {

    public static final String HRASEARCH = "searchtext";
    private SearchBoxView mSearchView;
    private TextView tv_hra_search_result;
    private LinearLayout ll_search;
    private int flags;
    private String searchText;
    private TextView tv_hra_search;
    private ImageView iv_order_search_back;
    private LinearLayout hra_search_hint;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_hra_search;
    }

    @Override
    public void initView() {
        mSearchView = (SearchBoxView) findViewById(R.id.search_toolbar);
        tv_hra_search_result = (TextView) findViewById(R.id.tv_hra_search_result);
        ll_search = (LinearLayout) findViewById(R.id.ll_search);
        tv_hra_search = (TextView) findViewById(R.id.tv_hra_search);  //搜索按钮
        iv_order_search_back = (ImageView) findViewById(R.id.iv_order_search_back);  //返回按钮
        hra_search_hint = (LinearLayout) findViewById(R.id.hra_search_hint);
        setToolbarVisible(false);
        if(mSearchView.getEditText().isFocusable()){
           softKey(mSearchView.getEditText());
        }
        tv_hra_search.setEnabled(false);
        getExtra();
        initEvent();
        initNameEt(mSearchView.getEditText());
    }

    public void getExtra(){
        Intent intent = getIntent();
        flags = intent.getFlags();
    }

    public void initEvent(){
        mSearchView.setRightClickMode(SearchBoxView.USERIF);
        ll_search.setOnClickListener(this);
        mSearchView.getEditText().addTextChangedListener(this);
        tv_hra_search.setOnClickListener(this);
        iv_order_search_back.setOnClickListener(this);
    }

    @Override
    public void onToolbarBackClick() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //点击开始搜索
            case R.id.ll_search:
                startResultActivity(flags);
                break;
            //点击返回到上一个界面
            case R.id.iv_order_search_back:
                finish();
                break;

            //点击搜索按钮
            case R.id.tv_hra_search:
                startResultActivity(flags);
                break;
        }
    }

    //通过标识判断搜索出来的结果
    public void startResultActivity(int flags){
        switch (flags){
            //搜索出来的是我的改签
            case MineEndorseFragment.MINEENDORSE:
                Intent intent1 = new Intent(this,EndorseSearchActivity.class);
                intent1.putExtra(HRASEARCH,searchText);
                startActivity(intent1);
                finish();
                break;
            //搜索出来的是我的预约
            case MineAppointFragment.MINEAPPOINT:
                Intent intent = new Intent(this,OrderSearchActivity.class);
                intent.putExtra(HRASEARCH,searchText);
                startActivity(intent);
                finish();
                break;

        }
    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        searchText = mSearchView.getText().toString().trim();
        if (!TextUtils.isEmpty(searchText)) {
            tv_hra_search.setEnabled(true);
            hra_search_hint.setVisibility(View.VISIBLE);
            tv_hra_search_result.setText(Html.fromHtml("搜索 " + "&#34"+searchText + "&#34" + " 的HRA预约"));
        } else {
            tv_hra_search.setEnabled(false);
            hra_search_hint.setVisibility(View.GONE);
        }
    }

    private void softKey(EditText editText) {
        try {
            editText.requestFocus();
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 姓名输入框只能输入汉字 并且少于15个汉字
     */
    private void initNameEt(EditText et_name) {
        et_name.setInputType(InputType.TYPE_CLASS_TEXT);
        et_name.setFilters(new InputFilter[]{
                new InputFilter() {
                    @Override
                    public CharSequence filter(CharSequence source, int start, int end, Spanned dest,
                                               int dstart, int dend) {
                        String dests = dest.toString(); //已经显示的字符串
                        int destslen = 0;
                        if (!TextUtils.isEmpty(dests)) {
                            destslen = dests.length();
                        }
                        String s = dest.toString() + source;
                        if (destslen >= 15) {  //已经显示的字符串长度如果大于15 则不能输入
                            return "";
                        } else {
                            if ((s.length() <= 15) && (source.toString().matches("[\u4e00-\u9fa5]+"))) { //要输入的字符串长度+已经显示的字符串长度<=15
                                return source;
                            } else {
                                if ((!TextUtils.isEmpty(source)) &&
                                        (source.toString().matches("[\u4e00-\u9fa5]+"))) {
                                    String inputs = source.toString().substring(0, 15 - destslen);
                                    return inputs;
                                }
                            }
                        }
                        return "";
                    }
                }
        });

    }
}
